import axios from 'axios';
import router from '../router/index'
axios.defaults.withCredentials=true;
// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        withCredentials:true,
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json',
        },
        data: params,
        traditional: true
    }).then(res => res.data);
};
// 通用公用方法
// const req = (method, url, params) => {
//     return axios({
//         method: method,
//         url: url,
//         headers: {
//             'Content-Type': 'application/json',
//             tokenId: localStorage.getItem('tokenId')
//         },
//         data: params,
//         traditional: true
//     }).then(res => res.data);
// };
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json',
            tokenId: localStorage.getItem('tokenId')
        },
        data: params,
        traditional: true
    }).then(res => {
        if(res.data.code == '100'){
            //如果请求成功就让他2秒跳转路由
            if(localStorage.getItem('tokenId')){
                localStorage.removeItem('tokenId')
            } 
            router.push({ path: '/login' })  
        }
        return res.data;
    });
};

const formreq = (method,url,params) => {
    return axios({
            method: method,
            url: url,
            headers: {
                'Content-Type': 'multipart/form-data',
                tokenId: localStorage.getItem('tokenId')
            },
            data: params,
            traditional: true
    }).then(res => {
        if(res.data.code == '100'){
            //如果请求成功就让他2秒跳转路由
            if(localStorage.getItem('tokenId')){
                localStorage.removeItem('tokenId')
            } 
            router.push({ path: '/login' })  
        }
        return res.data;
    });
}
export {
    loginreq,
    req,
    formreq
}